package com.ldg.gateway.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * 类名：
 * 功能：自定义局部过滤器
 */
@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {
	//构造函数
	public LogGatewayFilterFactory() {
		super(Config.class);
	}

	@Override
	public GatewayFilter apply(Config config) {
		return (exchange, chain) -> {
			if (config.isCacheLog()) {
				System.out.println("缓冲过滤器开启了。");
			}
			if (config.isConsoleLog()) {
				System.out.println("控制台过滤器开启了。");
			}
			return chain.filter(exchange);
		};
	}

	// 定义字段的顺序
	@Override
	public List<String> shortcutFieldOrder() {
		return Arrays.asList("consoleLog", "cacheLog");
	}

	@Data
	@AllArgsConstructor
	@NoArgsConstructor
	public static class Config {
		private boolean consoleLog;
		private boolean cacheLog;
	}
}
